package template.page.user;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import org.apache.click.control.Checkbox;
import org.apache.click.control.Column;
import org.apache.click.control.FieldSet;
import org.apache.click.control.Form;
import org.apache.click.control.Option;
import org.apache.click.control.PasswordField;
import org.apache.click.control.Submit;
import org.apache.click.control.Table;
import org.apache.click.control.TextField;
import org.apache.click.dataprovider.DataProvider;
import org.apache.click.extras.control.EmailField;
import org.apache.click.extras.control.PickList;

import template.page.BorderPage;
import cvcg.model.Game;
import cvcg.model.Player;

@SuppressWarnings("serial")
public class UserDetail extends BorderPage {

	public String title = "My details";
	protected Form playerForm = new Form("playerForm");	
	protected PickList ownedGamesPickList = new PickList("ownedGamesPickList","");
	protected PickList ruleMasteredGamesPickList = new PickList("ruleMasteredGamesPickList","");
	protected TextField nameField = new TextField("name", false);
	protected Submit saveSubmit = new Submit("save", " Save ", this, "onSaveClicked");
	protected FieldSet detailFieldSet = new FieldSet("Details");
	protected Checkbox multiTableCheckbox = new Checkbox("multiTable");
	protected Table highScoreTable = new Table("highScore");
	String loggedUser;
	
	public UserDetail() 
	{		
		addControl(playerForm);		
		addControl(highScoreTable);
		buildHighScoreTable();
		playerForm.getTag();
		ownedGamesPickList.setHeaderLabel("Games", "Owned");
		ownedGamesPickList.setHeight(15);				
		ruleMasteredGamesPickList.setHeaderLabel("Games", "Rules Known");
		ruleMasteredGamesPickList.setHeight(15);
		       
        FieldSet ownedGamesFieldSet = new FieldSet("Owned games");
        buildFieldSetStyle(ownedGamesFieldSet);
        FieldSet ruleMasteredGamesFieldSet = new FieldSet("Rule mastered games");
        buildFieldSetStyle(ruleMasteredGamesFieldSet);
        buildFieldSetStyle(detailFieldSet);
        playerForm.add(detailFieldSet);
        playerForm.add(ownedGamesFieldSet);
        playerForm.add(ruleMasteredGamesFieldSet);
                
        nameField.setReadonly(true);
        nameField.setDisabled(true);
        nameField.setMinLength(4);        
        
        detailFieldSet.add(nameField);
        detailFieldSet.add(new EmailField("email", true));
        detailFieldSet.add(new PasswordField("password",true));
        detailFieldSet.add(multiTableCheckbox);
               
        ownedGamesFieldSet.add(ownedGamesPickList);
        
        ruleMasteredGamesFieldSet.add(ruleMasteredGamesPickList);
        
        playerForm.add(saveSubmit);       
	}
	
    @Override
    public void onGet() 
    {
		Player player = gameSessionService.getPlayerByName(loggedUser);
        if (player != null) 
        {
            // Copy customer data to form. The idField value will be set by
            // this call
        	playerForm.copyFrom(player);            	
        	title = title + ": " + player.getName();
        	
        	for(Game game : player.getOwnedGames()) 
        	{
        		if (game.getId()!=null) 
        		{
        			ownedGamesPickList.addSelectedValue(game.getId().toString());
        		}
        	}
        	for(Game game : player.getRuleMasteredGames()) 
        	{
        		if (game.getId()!=null) 
        		{
        			ruleMasteredGamesPickList.addSelectedValue(game.getId().toString());
        		}
        	}     
        }
    }

	@SuppressWarnings({"rawtypes"})
	@Override
	public void onInit() 
	{
		super.onInit();
		
		loggedUser = getCurrentUser();
		DataProvider gamePickListDataProvider = new DataProvider() 
		{
			public List getData() 
			{
		        List<Game> gameList = gameSessionService.getAllGames();
		        List<Option> allGameOptions = new ArrayList<Option>();
		        
		        for (Game game : gameList) {
					allGameOptions.add(new Option(game.getId().toString(),game.getName()));
				}
				return allGameOptions;
			}
		};
		ownedGamesPickList.setDataProvider(gamePickListDataProvider);
		ruleMasteredGamesPickList.setDataProvider(gamePickListDataProvider);		
	}
	
	/**
     * Handle the OK button click event.
     *
     * @return true
     */
    public boolean onSaveClicked() 
    {
        if (playerForm.isValid()) 
        {
        	Player player = gameSessionService.getPlayerByName(loggedUser);
        	if (player!=null) 
        	{        	
        		gameSessionService.savePlayer(player.getId(), playerForm, ownedGamesPickList, ruleMasteredGamesPickList,null);
        	}
        	setRedirect(UserDetail.class);
        	return true;
        }        
        return false;
    }
    
   
	@SuppressWarnings("rawtypes")
	private void buildHighScoreTable() 
    {
    	highScoreTable.setClass(Table.CLASS_ITS);
    	highScoreTable.setShowBanner(false);
    	highScoreTable.setSortable(false);
    		
    	highScoreTable.addColumn(new Column("gameName"));
		Column points = new Column("points");
		points.setTextAlign("center");
		highScoreTable.addColumn(points);
		highScoreTable.setSortedColumn("gameName");
	
		highScoreTable.setDataProvider(new DataProvider() {
	
			public Collection getData() 
			{
				loggedUser = getCurrentUser();
				return gameSessionService.getScoreByPlayerName(loggedUser);
	
			}
		});
    }
    
    private void buildFieldSetStyle(FieldSet fieldSet) 
    {
    	fieldSet.setStyle("width", "95%");
    	fieldSet.setStyle("margin-bottom", "20px");
    	fieldSet.setStyle("padding", "15px");
    	fieldSet.setLegendAttribute("style", "font-weight:bold");
    }
}